import json
import time
import requests
def start(prompt):
    url='https://dashscope.aliyuncs.com/api/v1/services/aigc/text2image/image-synthesis'
    headers={
        'Content-Type': 'application/json',
        'Authorization':'Bearer sk-9b31f687f4e343d2b8bd808a98253aa1',
        'X-DashScope-Async':'enable'
    }
    data={
        'model':'wanx-v1',
        'input':{
            'prompt':prompt,
            # 'negative_prompt':''
        }
    }
    res=requests.post(url,headers=headers,data=json.dumps(data))
    print(res.text)
    id=res.json()['output']['task_id']

    #通过id查询生成的图片
    for i in range(1,4):
        time.sleep(20)
        url=f'https://dashscope.aliyuncs.com/api/v1/tasks/{id}'
        response=requests.get(url,headers=headers)
        print(response.text)
        try:
            img_url=response.json()['output']['results'][0]['url']
        except:
            continue
        print(img_url)
        image=requests.get(img_url)
        id='百炼'+str(id)
        with open(f'static/images/{id}.jpg','wb') as f:
            f.write(image.content)
            f.close()
        break
    return id